// 注册验证
exports.validateRegister = (req, res, next) => {
  const { username, password, email } = req.body;
  
  if (!username || !password || !email) {
    return res.status(400).json({
      success: false,
      message: '所有字段都是必填的'
    });
  }

  if (username.length < 3 || username.length > 20) {
    return res.status(400).json({
      success: false,
      message: '用户名长度必须在3-20个字符之间'
    });
  }

  if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
    return res.status(400).json({
      success: false,
      message: '无效的邮箱格式'
    });
  }

  if (password.length < 6) {
    return res.status(400).json({
      success: false,
      message: '密码长度至少6位'
    });
  }

  next();
};

// 登录验证
exports.validateLogin = (req, res, next) => {
  const { username, password } = req.body;
  if (!username || !password) {
    return res.status(400).json({ message: '所有字段都是必填的' });
  }
  next();
};

// 名片夹验证
exports.validateFolder = (req, res, next) => {
  const { name } = req.body;
  
  if (!name) {
    return res.status(400).json({
      success: false,
      message: '名片夹名称是必填的'
    });
  }

  if (name.length < 2 || name.length > 50) {
    return res.status(400).json({
      success: false,
      message: '名片夹名称长度必须在2-50个字符之间'
    });
  }

  next();
};

// 名片验证
exports.validateCard = (req, res, next) => {
  const { name, phone, email } = req.body;
  
  if (!name) {
    return res.status(400).json({
      success: false,
      message: '姓名是必填的'
    });
  }

  if (phone && !/^[0-9]{10,15}$/.test(phone)) {
    return res.status(400).json({
      success: false,
      message: '无效的电话号码格式'
    });
  }

  if (email && !/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
    return res.status(400).json({
      success: false,
      message: '无效的邮箱格式'
    });
  }

  next();
};
